package com.duanjw.springframework.scope;

import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 单例bean中获取原型bean
 *
 * @author duanjw
 */
@Component
public class SingletonWithPrototypeBean2 {
    private SingletonBean2 singletonBean2;

    public SingletonWithPrototypeBean2(SingletonBean2 singletonBean2) {
        this.singletonBean2 = singletonBean2;
    }

    public void show() {
        singletonBean2.show();
    }
}

/**
 * 单例bean
 * 不配置scope默认就是单例
 *
 * @author duanjw
 */
@Component
abstract class SingletonBean2 {

    /**
     * @return
     */
    @Lookup
    abstract PrototypeBean2 getPrototypeBean2();

    public void show() {
        System.out.println("单例bean：（bean是同一个）" + this.hashCode());
        System.out.println("原型bean：（bean不是同一个）" + getPrototypeBean2().hashCode());
        System.out.println("-----------");
    }
}

/**
 * 原型bean
 *
 * @author duanjw
 */
@Scope("prototype")
@Component
class PrototypeBean2 {

}
